// merchants/holderCoupon/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeofCard: 0, //0会员卡，1优惠券
    holderCard: {},
    holderCostcard: [],
    couponTypeList: [],
    gradeid: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.gradeid) {
      this.setData({
        gradeid: options.gradeid,
      })
      this.getGradePackageInfo(options.gradeid);
    }
    if(options.typeofCard){
      this.setData({
        typeofCard: options.typeofCard,
      })
    }
  },
  addProjectshare: function() {
    wx.navigateTo({
      url: '../addProjectShare/index?gradeid=' + this.data.gradeid,
    })
  },
  getGradePackageInfo: function(gradeid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetGradePackageInfo?gradeId=' + gradeid,
      success: function(res) {
        if (res.data.Code == 0) {
          let holderCard = {};
          let holderCostcard = [];
          let couponTypeList = res.data.Data.couponTypeList;
          if (res.data.Data.card != null) {
            holderCard = res.data.Data.card;
            let year = holderCard.EndTime.substr(0, 4);
            let month = holderCard.EndTime.substr(5, 2);
            let date = holderCard.EndTime.substr(8, 2);
            holderCard.EndTime = year + "/" + month + "/" + date;
            holderCard.Discount = holderCard.Discount * 0.1;
            holderCard.Discount = holderCard.Discount.toFixed(1);
            holderCard.CardNo = holderCard.CardNo.replace(/\s/g, '').replace(/(.{4})/g, "$1");
          } else {
            let year = res.data.ServerTime.substr(0, 4);
            let month = res.data.ServerTime.substr(5, 2);
            let date = res.data.ServerTime.substr(8, 2);
            holderCard.EndTime = year + "/" + month + "/" + date;
            holderCard.Title = "VIP会员卡";
            holderCard.Discount = 10;
            holderCard.CardNo = '0000 0000 0000 0000';
          }
          if (res.data.Data.cardItemList) {
            holderCostcard = res.data.Data.cardItemList;
          }
          if (res.data.Data.couponTypeList) {
            couponTypeList = res.data.Data.couponTypeList;
            let date = new Date();
            for (let i = 0; i < couponTypeList.length; i++) {
              var date2 = new Date(Date.parse(couponTypeList[i].sendendtime.replace(/-/g, "/")));
              if (date2 < date) {
                couponTypeList[i].isExpiretime = 1;
              } else {
                couponTypeList[i].isExpiretime = 0;
              }
              couponTypeList[i].sendstarttime = couponTypeList[i].sendstarttime.substr(0, 10);
              couponTypeList[i].sendendtime = couponTypeList[i].sendendtime.substr(0, 10);
            }
          }
          that.setData({
            holderCard: holderCard,
            holderCostcard: holderCostcard,
            couponTypeList: couponTypeList
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  changeType: function(e) {
    let typeofCard = e.currentTarget.dataset.typeofcard;
    this.setData({
      typeofCard: typeofCard
    })
  },
  // 修改卡项目
  updateCardItem: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../addProjectShare/index?cid=' + id + '&gradeid=' + this.data.gradeid,
    })
  },

  // 删除卡项目
  deleteCardItem: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认删除？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Coupon/DeleteCardItem?id=' + id + '&uid=' + app.globalData.userInfo.Uid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '删除成功',
                  duration: 2000
                })
                that.getGradePackageInfo(that.data.gradeid);
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },
  // 关闭优惠券
  updateCouponState: function (e) {
    let that = this;
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let state = e.currentTarget.dataset.state;
    wx.showModal({
      title: '提示',
      content: '作废后不可恢复，是否确认？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Coupon/ChangeCouponTypeState?couponTypeId=' + coupontypeid + '&state=' + state,
            success: function (res) {
              if (res.data.Code == 0) {
                if (!state) {
                  wx.showToast({
                    title: '优惠券已关闭',
                    duration: 2000
                  })
                }
                that.getGradePackageInfo(that.data.gradeid);
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },
  // 删除优惠券
  deleteCoupon: function (e) {
    let that = this;
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    wx.showModal({
      title: '提示',
      content: '删除后，已发放的该张优惠券失效，是否操作？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Coupon/DeleteCouponType?couponTypeId=' + coupontypeid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '删除成功',
                  duration: 1000
                })
                that.getGradePackageInfo(that.data.gradeid);
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },
  toDetail: function (e) {
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/merchant/merchants/addCoupon/index?coupontypeid=' + coupontypeid + '&type=' + type + '&gradeid=' + this.data.gradeid,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getGradePackageInfo(this.data.gradeid)
  },


})